﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Collections.ObjectModel;
using System.Xml;

namespace GamePlugin.Logics.Data
{
    public class PropertyItem : DependencyObject
    {
        private string _Name;
        public PropertyItem(string name)
        {
            _Name = name;
            this.Children = new ObservableCollection<string>();
        }

        public static readonly DependencyProperty ChildrenProperty =
            DependencyProperty.Register("Children", typeof(ObservableCollection<string>), typeof(PropertyItem), null);
        public ObservableCollection<string> Children
        {
            get { return (ObservableCollection<string>)GetValue(ChildrenProperty); }
            set { SetValue(ChildrenProperty, value); }
        }

        public void ReadXml(XmlNode root)
        {
            Children.Clear();

            root = root.SelectSingleNode("./" + "PropertyItem");

            var countNode = root.SelectSingleNode("./" + "PropertyItemCount");
            int count = int.Parse(countNode.InnerText);

            for (int i = 0; i < count; i++)
            {
                var node = root.SelectSingleNode("./" + "PropertyItemName" + i.ToString());
                Children.Add(node.InnerText);
            }
        }

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteStartElement("PropertyItem");

            writer.WriteStartElement("PropertyItemCount");
            writer.WriteString(Children.Count.ToString());
            writer.WriteFullEndElement();

            for (int i = 0; i < Children.Count; i++)
            {
                writer.WriteStartElement("PropertyItemName" + i.ToString());
                writer.WriteString(Children[i]);
                writer.WriteFullEndElement();
            }

            writer.WriteFullEndElement();
        }
    }
}
